package com.xdtech.trans.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xdtech.sys.aspect.SystemControllerLog;
import com.xdtech.sys.util.SessionContextUtil;
import com.xdtech.sys.vo.UserItem;
import com.xdtech.trans.model.TransAccount;
import com.xdtech.trans.model.TransWebSite;
import com.xdtech.trans.service.TransAccountService;
import com.xdtech.trans.service.TransWebSiteService;
import com.xdtech.trans.vo.PersonItem;
import com.xdtech.trans.vo.TransAccountItem;
import com.xdtech.web.model.ComboBox;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;

/**
 * 
 * @author max.zheng
 * @create 2015-06-06 17:27:45
 * @since 1.0
 * @see
 */
@Controller
@Scope("prototype")
public class TransAccountController {
	private Log log = LogFactory.getLog(TransAccountController.class);
	@Autowired
	private TransAccountService transAccountService;
	@Autowired
	private TransWebSiteService transWebSiteService;
	@RequestMapping(value="/transAccount.do",params = "transAccount")
	public ModelAndView skipTransAccount() {
		return new ModelAndView("trans/transAccount/transAccount_ftl");
	}
	
	@RequestMapping(value="/transAccount.do",params="loadList")
	@ResponseBody
	public Map<String, Object> loadList(TransAccountItem item,Pagination pg) {
		UserItem userItem = SessionContextUtil.getCurrentUser();
		if (userItem!=null&&!userItem.isSuperuser()) {
			if (userItem instanceof PersonItem) {
				//物流员工
				PersonItem personItem = (PersonItem) userItem;
				String websiteIds = transWebSiteService.getPersonSubWebsiteIdsById(Long.valueOf(personItem.getWebsiteId()));
				item.setWebsiteIds(websiteIds);
			}else {
				item.setWebsiteIds("-1");
			}
		}
		Map<String, Object> results =  transAccountService.loadPageDataByConditions(pg,item,"findTransAccountByCondition");
		return results;
	}
	
	@RequestMapping(value="/transAccount.do",params = "editTransAccount")
	@SystemControllerLog(description = "编辑")
	public ModelAndView editTransAccount(HttpServletRequest request,Long transAccountId) {
		if (transAccountId!=null) {
			request.setAttribute("disabled", "true");
			request.setAttribute("transAccountItem", transAccountService.loadTransAccountItem(transAccountId));
		}else {
			request.setAttribute("disabled", "false");
		}
		return new ModelAndView("trans/transAccount/editTransAccount_ftl");
	}
	
	@RequestMapping(value="/transAccount.do",params = "skip")
	public ModelAndView skip(HttpServletRequest request,String page,Long transAccountId) {
		if (transAccountId!=null) {
			request.setAttribute("transAccountItem", transAccountService.loadTransAccountItem(transAccountId));
		}
		return new ModelAndView("trans/transAccount/"+page+"_ftl");
	}
	
	@RequestMapping(value="/transAccount.do",params = "saveTransAccount")
	@ResponseBody
	@SystemControllerLog(description = "保存")
	public ResultMessage saveTransAccount(TransAccountItem item) {
		ResultMessage r = new ResultMessage();
		if (transAccountService.saveOrUpdateTransAccount(item)) {
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/transAccount.do",params = "deleteTransAccountItems")
	@ResponseBody
	@SystemControllerLog(description = "删除")
	public ResultMessage deleteTransAccountItems(String ids) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(ids)) {
			String[] tempIds = ids.split(",");
			List<Long> transAccountIds = new ArrayList<Long>();
			for (String id : tempIds) {
				transAccountIds.add(Long.valueOf(id));
			}
			transAccountService.deleteTransAccountInfo(transAccountIds);
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	/**
	 * 验证配置是否重复
	 * 
	 * @author max.zheng
	 * @create 2015-4-5下午10:12:54
	 * @modified by
	 * @param id
	 * @param roomType
	 * @return
	 */
	@RequestMapping(value="/transAccount.do",params = "checkAccountTypeIsExit")
	@ResponseBody
	public boolean checkAccountTypeIsExit(String websiteId,String accountType) {
		if (StringUtils.isBlank(websiteId)||StringUtils.isBlank(accountType))
		{
			return false;
		}else
		{
			boolean rs = transAccountService.accountTypeExitByWebSiteId(websiteId,accountType);
			return !rs;
		}
		
	}
	
	@RequestMapping(value="/transAccount.do",params = "loadAccountCombox")
	@ResponseBody
	public List<ComboBox> loadAccountCombox(String value,String text,String websiteId) {
		List<TransAccount> transAccounts = null;
		if (StringUtils.isNotBlank(websiteId))
		{
			transAccounts = transAccountService.getAccountsByWebsiteId(Long.valueOf(websiteId));
		}else
		{
			transAccounts = transAccountService.getAll();
		}
				
		List<ComboBox> comboBoxs = new ArrayList<ComboBox>();
		for (TransAccount ta : transAccounts)
		{
			comboBoxs.add(new ComboBox(ta.getId().toString(), ta.getAccountName(), ta.getId().toString().equals(value)||ta.getAccountName().equals(text)));
		}
		return comboBoxs;
	}

	

}
